﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Collections;
namespace DALL.LibrarySys
{
    public class T_PUB_SCRAP
    {
        public T_PUB_SCRAP()
        { }

        /// <summary>
        /// 获取报修智能提示
        /// </summary>
        /// <param name="branch_id"></param>
        /// <returns></returns>
        public DataSet GetRepairApplyTitel(int branch_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT TITEL FROM T_PUB_SCRAP ");
            strSql.Append(" where FLAG=2 and BRANCH_ID=" + branch_id);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public DataSet GetRepairApplyTitel()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT TITEL FROM T_PUB_SCRAP where FLAG=2");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获取报废智能提示
        /// </summary>
        /// <param name="branch_id"></param>
        /// <returns></returns>
        public DataSet GetScrapApplyTitel(int branch_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT TITEL FROM T_PUB_SCRAP ");
            strSql.Append(" where FLAG<>2 and BRANCH_ID=" + branch_id);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public DataSet GetScrapApplyTitel()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT TITEL FROM T_PUB_SCRAP where FLAG<>2");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetApplyList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,a.BRANCH_ID,a.LIBRARY_ID,MATERIAL_ID,MATERIAL_NAME,NUM,APPLICANT,STATE,REMARK,TITEL,APPLY_TIME,NOTATION,APPROVER,APPLICANT_NAME,APPROVER_NAME,FLAG,APPROVE_TIME,LIBRARY_NAME,BRANCH_NAME ");
            strSql.Append(" FROM T_PUB_SCRAP as a,  T_PUB_LIBRARY as b, T_PUB_BRANCH as c ");
            strSql.Append(" where  b.LIBRARY_ID = a.LIBRARY_ID and c.BRANCH_ID = a.BRANCH_ID ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 报销更新库存
        /// </summary>
        /// <param name="model"></param>
        /// <param name="lmmodel"></param>
        public void UpdateLibraryMaterial(Model.LibrarySys.T_PUB_SCRAP model, Model.LibrarySys.T_PUB_LIBRARYMATERIAL lmmodel)
        {
            ArrayList strArry = new ArrayList();

            #region 更新报销单
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_SCRAP set ");
            if (model.BRANCH_ID != null)
            {
                strSql.Append("BRANCH_ID=" + model.BRANCH_ID + ",");
            }
            else
            {
                strSql.Append("BRANCH_ID= null ,");
            }
            if (model.LIBRARY_ID != null)
            {
                strSql.Append("LIBRARY_ID=" + model.LIBRARY_ID + ",");
            }
            else
            {
                strSql.Append("LIBRARY_ID= null ,");
            }
            if (model.MATERIAL_ID != null)
            {
                strSql.Append("MATERIAL_ID=" + model.MATERIAL_ID + ",");
            }
            else
            {
                strSql.Append("MATERIAL_ID= null ,");
            }
            if (model.MATERIAL_NAME != null)
            {
                strSql.Append("MATERIAL_NAME='" + model.MATERIAL_NAME + "',");
            }
            else
            {
                strSql.Append("MATERIAL_NAME= null ,");
            }
            if (model.NUM != null)
            {
                strSql.Append("NUM=" + model.NUM + ",");
            }
            else
            {
                strSql.Append("NUM= null ,");
            }
            if (model.APPLICANT != null)
            {
                strSql.Append("APPLICANT=" + model.APPLICANT + ",");
            }
            else
            {
                strSql.Append("APPLICANT= null ,");
            }
            if (model.STATE != null)
            {
                strSql.Append("STATE='" + model.STATE + "',");
            }
            else
            {
                strSql.Append("STATE= null ,");
            }
            if (model.REMARK != null)
            {
                strSql.Append("REMARK='" + model.REMARK + "',");
            }
            else
            {
                strSql.Append("REMARK= null ,");
            }
            if (model.TITEL != null)
            {
                strSql.Append("TITEL='" + model.TITEL + "',");
            }
            else
            {
                strSql.Append("TITEL= null ,");
            }
            if (model.APPLY_TIME != null)
            {
                strSql.Append("APPLY_TIME='" + model.APPLY_TIME + "',");
            }
            else
            {
                strSql.Append("APPLY_TIME= null ,");
            }
            if (model.NOTATION != null)
            {
                strSql.Append("NOTATION='" + model.NOTATION + "',");
            }
            else
            {
                strSql.Append("NOTATION= null ,");
            }
            if (model.APPROVER != null)
            {
                strSql.Append("APPROVER=" + model.APPROVER + ",");
            }
            else
            {
                strSql.Append("APPROVER= null ,");
            }
            if (model.APPLICANT_NAME != null)
            {
                strSql.Append("APPLICANT_NAME='" + model.APPLICANT_NAME + "',");
            }
            else
            {
                strSql.Append("APPLICANT_NAME= null ,");
            }
            if (model.APPROVER_NAME != null)
            {
                strSql.Append("APPROVER_NAME='" + model.APPROVER_NAME + "',");
            }
            else
            {
                strSql.Append("APPROVER_NAME= null ,");
            }
            if (model.FLAG != null)
            {
                strSql.Append("FLAG='" + model.FLAG + "',");
            }
            else
            {
                strSql.Append("FLAG= null ,");
            }
            if (model.APPROVE_TIME != null)
            {
                strSql.Append("APPROVE_TIME='" + model.APPROVE_TIME + "',");
            }
            else
            {
                strSql.Append("APPROVE_TIME= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ID=" + model.ID + "");
            strArry.Add(strSql);
            #endregion

            #region 更新仓库材料表
            StringBuilder strSqlm = new StringBuilder();
            strSqlm.Append("update T_PUB_LIBRARYMATERIAL set ");
            if (lmmodel.LIBRARY_ID != null)
            {
                strSqlm.Append("LIBRARY_ID=" + lmmodel.LIBRARY_ID + ",");
            }
            else
            {
                strSqlm.Append("LIBRARY_ID= null ,");
            }
            if (lmmodel.MATERIAL_ID != null)
            {
                strSqlm.Append("MATERIAL_ID=" + lmmodel.MATERIAL_ID + ",");
            }
            else
            {
                strSqlm.Append("MATERIAL_ID= null ,");
            }
            if (lmmodel.NUM != null)
            {
                strSqlm.Append("NUM=" + lmmodel.NUM + ",");
            }
            else
            {
                strSqlm.Append("NUM= null ,");
            }
            if (lmmodel.MINVALUE != null)
            {
                strSqlm.Append("MINVALUE=" + lmmodel.MINVALUE + ",");
            }
            else
            {
                strSqlm.Append("MINVALUE= null ,");
            }
            int m = strSqlm.ToString().LastIndexOf(",");
            strSqlm.Remove(m, 1);
            strSqlm.Append(" where ID=" + lmmodel.ID + "");
            strArry.Add(strSqlm);
            #endregion

            DBHelper.ExecuteSqlTran(strArry);
        }

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_PUB_SCRAP where ID=@ID ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.LibrarySys.T_PUB_SCRAP model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_SCRAP(");
            strSql.Append("BRANCH_ID,LIBRARY_ID,MATERIAL_ID,MATERIAL_NAME,NUM,APPLICANT,STATE,REMARK,TITEL,APPLY_TIME,NOTATION,APPROVER,APPLICANT_NAME,APPROVER_NAME,FLAG,APPROVE_TIME)");

            strSql.Append(" values (");
            strSql.Append("@BRANCH_ID,@LIBRARY_ID,@MATERIAL_ID,@MATERIAL_NAME,@NUM,@APPLICANT,@STATE,@REMARK,@TITEL,@APPLY_TIME,@NOTATION,@APPROVER,@APPLICANT_NAME,@APPROVER_NAME,@FLAG,@APPROVE_TIME)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "LIBRARY_ID", DbType.Int32, model.LIBRARY_ID);
            db.AddInParameter(dbCommand, "MATERIAL_ID", DbType.Int32, model.MATERIAL_ID);
            db.AddInParameter(dbCommand, "MATERIAL_NAME", DbType.String, model.MATERIAL_NAME);
            db.AddInParameter(dbCommand, "NUM", DbType.Double, model.NUM);
            db.AddInParameter(dbCommand, "APPLICANT", DbType.Int32, model.APPLICANT);
            db.AddInParameter(dbCommand, "STATE", DbType.String, model.STATE);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            db.AddInParameter(dbCommand, "TITEL", DbType.String, model.TITEL);
            db.AddInParameter(dbCommand, "APPLY_TIME", DbType.DateTime, model.APPLY_TIME);
            db.AddInParameter(dbCommand, "NOTATION", DbType.String, model.NOTATION);
            db.AddInParameter(dbCommand, "APPROVER", DbType.Int32, model.APPROVER);
            db.AddInParameter(dbCommand, "APPLICANT_NAME", DbType.String, model.APPLICANT_NAME);
            db.AddInParameter(dbCommand, "APPROVER_NAME", DbType.String, model.APPROVER_NAME);
            db.AddInParameter(dbCommand, "FLAG", DbType.String, model.FLAG);
            db.AddInParameter(dbCommand, "APPROVE_TIME", DbType.DateTime, model.APPROVE_TIME);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.LibrarySys.T_PUB_SCRAP model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_SCRAP set ");
            strSql.Append("BRANCH_ID=@BRANCH_ID,");
            strSql.Append("LIBRARY_ID=@LIBRARY_ID,");
            strSql.Append("MATERIAL_ID=@MATERIAL_ID,");
            strSql.Append("MATERIAL_NAME=@MATERIAL_NAME,");
            strSql.Append("NUM=@NUM,");
            strSql.Append("APPLICANT=@APPLICANT,");
            strSql.Append("STATE=@STATE,");
            strSql.Append("REMARK=@REMARK,");
            strSql.Append("TITEL=@TITEL,");
            strSql.Append("APPLY_TIME=@APPLY_TIME,");
            strSql.Append("NOTATION=@NOTATION,");
            strSql.Append("APPROVER=@APPROVER,");
            strSql.Append("APPLICANT_NAME=@APPLICANT_NAME,");
            strSql.Append("APPROVER_NAME=@APPROVER_NAME,");
            strSql.Append("FLAG=@FLAG,");
            strSql.Append("APPROVE_TIME=@APPROVE_TIME");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, model.ID);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "LIBRARY_ID", DbType.Int32, model.LIBRARY_ID);
            db.AddInParameter(dbCommand, "MATERIAL_ID", DbType.Int32, model.MATERIAL_ID);
            db.AddInParameter(dbCommand, "MATERIAL_NAME", DbType.String, model.MATERIAL_NAME);
            db.AddInParameter(dbCommand, "NUM", DbType.Double, model.NUM);
            db.AddInParameter(dbCommand, "APPLICANT", DbType.Int32, model.APPLICANT);
            db.AddInParameter(dbCommand, "STATE", DbType.String, model.STATE);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            db.AddInParameter(dbCommand, "TITEL", DbType.String, model.TITEL);
            db.AddInParameter(dbCommand, "APPLY_TIME", DbType.DateTime, model.APPLY_TIME);
            db.AddInParameter(dbCommand, "NOTATION", DbType.String, model.NOTATION);
            db.AddInParameter(dbCommand, "APPROVER", DbType.Int32, model.APPROVER);
            db.AddInParameter(dbCommand, "APPLICANT_NAME", DbType.String, model.APPLICANT_NAME);
            db.AddInParameter(dbCommand, "APPROVER_NAME", DbType.String, model.APPROVER_NAME);
            db.AddInParameter(dbCommand, "FLAG", DbType.String, model.FLAG);
            db.AddInParameter(dbCommand, "APPROVE_TIME", DbType.DateTime, model.APPROVE_TIME);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_SCRAP ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.LibrarySys.T_PUB_SCRAP GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BRANCH_ID,LIBRARY_ID,MATERIAL_ID,MATERIAL_NAME,NUM,APPLICANT,STATE,REMARK,TITEL,APPLY_TIME,NOTATION,APPROVER,APPLICANT_NAME,APPROVER_NAME,FLAG,APPROVE_TIME from T_PUB_SCRAP ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            Model.LibrarySys.T_PUB_SCRAP model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BRANCH_ID,LIBRARY_ID,MATERIAL_ID,MATERIAL_NAME,NUM,APPLICANT,STATE,REMARK,TITEL,APPLY_TIME,NOTATION,APPROVER,APPLICANT_NAME,APPROVER_NAME,FLAG,APPROVE_TIME ");
            strSql.Append(" FROM T_PUB_SCRAP ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

      

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.LibrarySys.T_PUB_SCRAP> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BRANCH_ID,LIBRARY_ID,MATERIAL_ID,MATERIAL_NAME,NUM,APPLICANT,STATE,REMARK,TITEL,APPLY_TIME,NOTATION,APPROVER,APPLICANT_NAME,APPROVER_NAME,FLAG,APPROVE_TIME ");
            strSql.Append(" FROM T_PUB_SCRAP ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.LibrarySys.T_PUB_SCRAP> list = new List<Model.LibrarySys.T_PUB_SCRAP>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.LibrarySys.T_PUB_SCRAP ReaderBind(IDataReader dataReader)
        {
            Model.LibrarySys.T_PUB_SCRAP model = new Model.LibrarySys.T_PUB_SCRAP();
            object ojb;
            ojb = dataReader["ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ID = (int)ojb;
            }
            ojb = dataReader["BRANCH_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BRANCH_ID = (int)ojb;
            }
            ojb = dataReader["LIBRARY_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.LIBRARY_ID = (int)ojb;
            }
            ojb = dataReader["MATERIAL_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MATERIAL_ID = (int)ojb;
            }
            model.MATERIAL_NAME = dataReader["MATERIAL_NAME"].ToString();
            ojb = dataReader["NUM"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.NUM = Convert.ToInt32(ojb);
            }
            ojb = dataReader["APPLICANT"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.APPLICANT = (int)ojb;
            }
            model.STATE = dataReader["STATE"].ToString();
            model.REMARK = dataReader["REMARK"].ToString();
            model.TITEL = dataReader["TITEL"].ToString();
            ojb = dataReader["APPLY_TIME"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.APPLY_TIME = (DateTime)ojb;
            }
            model.NOTATION = dataReader["NOTATION"].ToString();
            ojb = dataReader["APPROVER"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.APPROVER = (int)ojb;
            }
            model.APPLICANT_NAME = dataReader["APPLICANT_NAME"].ToString();
            model.APPROVER_NAME = dataReader["APPROVER_NAME"].ToString();
            model.FLAG = dataReader["FLAG"].ToString();
            ojb = dataReader["APPROVE_TIME"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.APPROVE_TIME = (DateTime)ojb;
            }
            return model;
        }

        #endregion  Method
    }
}
